package com.mrjy.maintain.bo;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.mrjy.common.entity.SMSSendInfo;
import com.mrjy.common.entity.UserBalanceInfo;
import com.mrjy.common.entity.UserBalancePayment;
import com.mrjy.common.entity.UserInfo;
import com.mrjy.common.entity.UserMemberInfo;
import com.mrjy.common.util.DateUtil;
import com.mrjy.common.util.MD5Encrypt;
import com.mrjy.maintain.dao.IMessageInfoDao;
import com.mrjy.maintain.dao.IUserBalanceInfoDao;
import com.mrjy.maintain.dao.IUserInfoDao;
import com.mrjy.maintain.dao.IUserMemberInfoDao;
import com.mrjy.payment.dao.IUserBalancePaymentDao;

@Service("userBalanceInfoBo")
public class UserBalanceInfoBo implements IUserBalanceInfoBo {

	@Resource
	private IUserBalanceInfoDao userBalanceInfoDao;
	@Resource
	private IUserMemberInfoDao userMemberInfoDao;
	
	@Resource IUserInfoDao userInfoDao;
	
	@Resource IUserBalancePaymentDao userBalancePaymentDao;
	
	@Resource
	private IMessageInfoDao messageInfoDao;
	
	@Resource
	private ISMSSendInfoBo smsSendInfoBo;
	
	@Override
	public String insertUserBalance(UserBalanceInfo userBalanceInfo) {
		userBalanceInfoDao.insert(userBalanceInfo);
		return "ok";
	}

	@Override
	public int insertBatch(List<UserBalanceInfo> userBalanceInfos) {
		int row = 0;
		try {
			row = userBalanceInfoDao.insertBatch(userBalanceInfos);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return row;
	}

	@Override
	public Object queryByUserId(UserInfo userInfo) {
		if (userInfo != null) {
			UserBalanceInfo userBalanceInfo = userBalanceInfoDao.queryByUserId(userInfo.getId());
			if (userBalanceInfo == null) {
				UserBalanceInfo ubi = new UserBalanceInfo();
				ubi.setUserId(userInfo.getId());
				ubi.setMoney(0.0f);
				ubi.setPayPassword(MD5Encrypt.encryptByMD5(userInfo.getMobilePhone()));
				ubi.setMembershipFlag(0);
				userBalanceInfoDao.insert(ubi);
				ubi.setPayPassword(null);
				return ubi;
			}else {
				UserMemberInfo userMemberInfo = new UserMemberInfo();
				userMemberInfo.setUserId(userInfo.getId());
				userMemberInfo.setMembershipFlag(1);
				List<UserMemberInfo> userMemberInfos = userMemberInfoDao.queryBySelected(userMemberInfo);
				if (userMemberInfos != null && userMemberInfos.size()>0) {
					userBalanceInfo.setMembershipFlag(1);
					userBalanceInfo.setEndDateString(DateUtil.longToString(userMemberInfos.get(0).getEndDate(), DateUtil.YEAR_MONTH_DAY));
				}else {
					userBalanceInfo.setMembershipFlag(0);
				}
				userBalanceInfo.setPayPassword(null);
				return userBalanceInfo;
			}
		}else {
			return "userInfo_error";
		}
	}
	
	@Override
	public Object queryByMobielPhone(String  mobilePhone) {
		String flag="ok";
		UserInfo user=userInfoDao.queryByMobilePhone(mobilePhone);			
		if(user!=null){
			//userBalanceInfo = userBalanceInfoDao.queryByUserId(user.getId());
			UserBalancePayment userBalancePayment = new UserBalancePayment();
			userBalancePayment.setUserId(user.getId());		
			int payNum=userBalancePaymentDao.queryHadPaymentByUserId(userBalancePayment).size();
			if(payNum>0) flag="error";
		}
		
		return flag;
	}
	

	@Override
	public Object resetPayPW(UserInfo userInfo,UserBalanceInfo userBalanceInfo,String mobilePhoneCode) {
		if (userInfo == null) {
			return "userInfo_error";
		}
		SMSSendInfo smsSendInfo=new SMSSendInfo();
		smsSendInfo.setRecNum(userInfo.getMobilePhone());
		smsSendInfo.setSendCode(mobilePhoneCode);	
		smsSendInfo.setSmsCode("SMS_16440022");
		smsSendInfo=smsSendInfoBo.validateSMSCode(smsSendInfo);
		//String mark = SMSUtil.checkPhoneMsgCode(mobilePhoneCode, map);
		if ("ok".equals(smsSendInfo.getSmsMessage())) {
			UserBalanceInfo ubi = userBalanceInfoDao.queryByUserId(userInfo.getId());
			if (ubi == null) {
				ubi = new UserBalanceInfo();
				ubi.setUserId(userInfo.getId());
				ubi.setMoney(0.0f);
				ubi.setPayPassword(MD5Encrypt.encryptByMD5(userBalanceInfo.getPayPasswordString()));
				userBalanceInfoDao.insert(ubi);
			}else {
				ubi.setPayPassword(MD5Encrypt.encryptByMD5(userBalanceInfo.getPayPasswordString()));
				userBalanceInfoDao.updateById(ubi);	
			}
		}
		return smsSendInfo.getSmsMessage();
	}

	@Override
	public List<UserBalanceInfo> queryPageUserBalance(
			UserBalanceInfo userBalanceInfo) {
		return userBalanceInfoDao.queryPageUserBalance(userBalanceInfo);
	}

}
